//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web
{
    public partial class AddressEditPage : BaseNopFrontendPage
    {
        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(AddressEditPage_Load);
            btnSave.Click+=new EventHandler(btnSave_Click);
            btnDelete.Click+=new EventHandler(btnDelete_Click);
        }

        private void BindData() {
            AddressEditControl.IsBillingAddress = this.IsBillingAddress;
            if (this.AddressId > 0) {
                var address = this.CustomerService.GetAddressById(this.AddressId);
                if (address != null) {
                    lHeaderTitle.Text = GetLocaleResourceString("Address.UpdateAddressTitle");
                    btnSave.Text = GetLocaleResourceString("Address.UpdateAddress");
                    btnDelete.Visible = true;
                    AddressEditControl.IsNew = false;
                    AddressEditControl.IsBillingAddress = address.IsBillingAddress;
                    AddressEditControl.Address = address;
                }
                else
                    Response.Redirect(CommonHelper.GetStoreLocation());
            }
            else {
                lHeaderTitle.Text = GetLocaleResourceString("Address.NewAddressTitle");
                btnSave.Text = GetLocaleResourceString("Address.AddAddress");
                btnDelete.Visible = false;
                AddressEditControl.IsNew = true;
                AddressEditControl.IsBillingAddress = this.IsBillingAddress;
            }
        }

        void AddressEditPage_Load(object sender, EventArgs e) {
            string title = GetLocaleResourceString("PageTitle.AddressEdit");
            SEOHelper.RenderTitle(this, title, true);

            if (!CustomerGuid.Equals(Guid.Empty)) {

                CommonHelper.SetResponseNoCache(Response);

                if (NopContext.Current.User == null) {
                    string loginURL = SEOHelper.GetLoginPageUrl(true);
                    Response.Redirect(loginURL);
                }
                var address = this.CustomerService.GetAddressById(this.AddressId);

                // if we're editing an address
                if (address != null) {
                    // if the address's customer guid matches the query string guid
                    if (address.Customer.CustomerGuid.Equals(CustomerGuid)) {
                        var addressCustomer = address.Customer;
                        if (addressCustomer == null || addressCustomer.CustomerId != NopContext.Current.User.CustomerId) {
                            string loginURL = SEOHelper.GetLoginPageUrl(true);
                            Response.Redirect(loginURL);
                        }

                        if (DeleteAddress) {
                            this.CustomerService.DeleteAddress(address.AddressId);
                            Response.Redirect(SEOHelper.GetMyAccountUrl());
                        }
                    }
                }

                // editing or adding ... let it bind
                if (!Page.IsPostBack) {
                    this.BindData();
                }

            } else {
                string loginURL = SEOHelper.GetLoginPageUrl(true);
                Response.Redirect(loginURL);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            Page.Validate("AddressEditValidationGroup");
            if (Page.IsValid) {
                var oldAddress = this.CustomerService.GetAddressById(this.AddressId);
                var inputedAddress = AddressEditControl.Address;
                if (oldAddress != null) {
                    oldAddress.FirstName = inputedAddress.FirstName;
                    oldAddress.LastName = inputedAddress.LastName;
                    oldAddress.PhoneNumber = inputedAddress.PhoneNumber;
                    oldAddress.FaxNumber = inputedAddress.FaxNumber;
                    oldAddress.MobilePhoneNumber = inputedAddress.MobilePhoneNumber;
                    oldAddress.Email = inputedAddress.Email;

                    // if it's a legal entity
                    if (inputedAddress.CompanyLegalForm) {
                        oldAddress.CompanyLegalForm = inputedAddress.CompanyLegalForm;
                        oldAddress.Company = inputedAddress.Company;
                        oldAddress.CompanyFiscalCode = inputedAddress.CompanyFiscalCode;
                        oldAddress.CompanyRegComNumber = inputedAddress.CompanyRegComNumber;
                        oldAddress.CompanyBank = inputedAddress.CompanyBank;
                        oldAddress.CompanyBankAccount = inputedAddress.CompanyBankAccount;
                    }

                    oldAddress.FullAddress = inputedAddress.FullAddress;
                    oldAddress.City = inputedAddress.City;
                    oldAddress.StateProvinceId = inputedAddress.StateProvinceId;
                    oldAddress.ZipPostalCode = inputedAddress.ZipPostalCode;
                    oldAddress.CountryId = inputedAddress.CountryId;
                    oldAddress.UpdatedOn = DateTime.UtcNow;

                    this.CustomerService.UpdateAddress(oldAddress);
                } else {
                    inputedAddress.CustomerId = NopContext.Current.User.CustomerId;
                    inputedAddress.IsBillingAddress = this.IsBillingAddress;
                    inputedAddress.CreatedOn = DateTime.UtcNow;
                    inputedAddress.UpdatedOn = DateTime.UtcNow;
                    this.CustomerService.InsertAddress(inputedAddress);
                }
                Response.Redirect(SEOHelper.GetMyAccountUrl());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e) {
            this.CustomerService.DeleteAddress(this.AddressId);
            Response.Redirect(SEOHelper.GetMyAccountUrl());
        }

        public Guid CustomerGuid {
            get { return CommonHelper.QueryStringGuid("CustomerGuid").HasValue ? CommonHelper.QueryStringGuid("CustomerGuid").Value : Guid.Empty; }
        }

        public int AddressId {
            get {
                return CommonHelper.QueryStringInt("AddressId");
            }
        }

        public bool IsBillingAddress {
            get {
                return CommonHelper.QueryStringBool("IsBillingAddress");
            }
        }

        public bool DeleteAddress {
            get {
                return CommonHelper.QueryStringBool("Delete");
            }
        }

        public override PageSslProtectionEnum SslProtected {
            get {
                return PageSslProtectionEnum.Yes;
            }
        }
    }
}